Demos:
Corrections:
My grand-daughter wanted a convenient list, so she would not forget to send a Valentine card to all her classmates. Personal computers are ideally suited for lists, whether it's your stamp collection, coin collection, video tape collection, team scores, or your church fund raising activities. It's also suited to Katy's Valentine list.
Katy's teacher sent home a list of her classmates which I put into DATA statements. (Why didn't I just give Katy the list? Where's the fun in that? How do I learn programming without doing?) I DO NOT recommend hard-coding data into a program unless it is essential to initialize variables. DATA statements cannot be altered by a user unfamiliar with programming, especially if you have created a stand-alone application and tokenized the BAS program.
DATA statements can be scattered anywhere in a program, but most coders like to place them all at the top, or bottom, of the program. Here are my DATA statements.
DATA "Katy G", "Bill J", "Eddie V", "Charlie G", "Monica D"
DATA "Ralph K", "Henry C", "Linda R", "Gloria A", "Pam F"
DATA "Dennis B", "Susan L", "Brenda S", "Tyler H", "Carlos E"
DATA "Zzyzx"
Zzyzx? Why would anyone name a child Zzyzx? As far as I know, no one has ever had the misfortune to be named Zzyzx (pronounced "Sisk"), except a small town near the California/Nevada border. I added Zzyzx as a marker (called a "sentinel") to indicate when all the DATA has been READ into my program. Any value which you are fairly certain will not be entered into your program may be used as a sentinel, even "thisIsTheEnd."
DATA is often READ into a program using a loop to READ all DATA elements. Because I'm writing this program, I know Katy has 15 classmates and I could use a FOR/NEXT loop with 15 iterations, but that will not allow entering more names unless I also update the FOR/NEXT loop every time a new student joins Katy's class.
To keep this program simple, yet flexible, I'll collect my data with a WHILE/WEND loop, although a DO/UNTIL or DO/WHILE loop could also be used. The simplest routine I can write might look like this:
WHILE name$<>"Zzyzx"
READ name$
PRINT name$
WEND
As soon as I ran the program, Katy asked, "Who is Zzyzx?" How do grand-father's get into these predicaments? "Shush, dear. I'll make him go away."
I add two lines of code and my simple program looks like this:
WHILE name$<>"Zzyzx"
READ name$
IF name$="Zzyzx" THEN EXIT WHILE
PRINT name$
WEND
As soon as the data is read into the program and name$ is compared to "Zzyzx," the program exits the loop to END, and the mysterious classmate is not printed to the list; neither does the program crash with an "Out of data" message.
Okay, that looks pretty good, as far as we've gone. I don't need to know how many classmates Katy has, but if I decide to sort her list, I have to count them so I will know how large to DIMension an array for sorting.
Arrays are easily DIMensioned or REDIMensioned anywhere within a program. If you REDIM your array after loading, the values of every element will be lost and you will have to load the array again. It's best to DIM your array at the beginning of the program and avoid a "Subscript out of range" crash. How large should I make the array? I know I have 15 students in the list now, but what if it grows to 25, or 35, or more? Why not DIM the array to 100 and hedge all bets?
Because that's sloppy programming. In the days when PC's had only 4K of RAM, you would not want to waste a single available byte. Remember, the computer on board the first Apollo Mission to land on the moon had only 3.3K of RAM, and it was called upon to perform many more tasks than this little Valentine list.
In the interest of good programming, let's write the code to allow the computer to count all the data items (sounds like an excellent idea in the event we are listing all 2,367 cards in our baseball collection!) and use the value returned to DIMension our array.
'Count the data items.
WHILE name$<>"Zzyzx"
READ temp$
IF temp$="Zzyzx" THEN EXIT WHILE
cntr=cntr+1
WEND
'Load the array.
[loadArray]
DIM name$(cntr)
RESTORE
FOR k=1 to cntr
READ temp$
name$(k)=temp$
NEXT k
'Sort the array.
FOR i = 1 TO cntr
FOR k = 1 TO (cntr - 1)
IF name$(k) > name$(k + 1) THEN
hold$ = name$(k) 'These three lines
name$(k) = name$(k + 1) 'swap strings if value
name$(k + 1) = hold$ 'is out of order.
END IF
NEXT k
NEXT i
'Print the sorted list
FOR p=1 to cntr
PRINT name$(p)
NEXT p
The RESTORE statement is necessary because all the DATA has been READ while counting the items and the program will crash with an "Out of data" error if we try to read it again. RESTORE allows us to READ the DATA for loading into the array. We've also included name$() array to hold our list for sorting, and changed the original READ name$ to READ temp$, because arrays cannot be loaded directly from the main program. That's okay if you're loading data from a file, but when loading from data statements in the program, you must first load them to a temporary variable.
The IF/THEN comparison must be made after the READ statement, or else there is no value to be compared. The IF/THEN should be made before the cntr is incremented, or we will be including the sentinel in the array DIM. This might also cause an "Out of data" crash elsewhere in the program when SORTing or PRINTing because the cntr value will seek one item not loaded into the array..
After loading name$() array, it is SORTed with a simple "bubble sort". [Editor: LB supports the SORT function, however Bill's code allows common usage across all platforms offered by ShopTalk Systems.] Admittedly, the bubble sort is the slowest of all, but it is also the easiest to grasp. You may choose from many other algorithms which are available from many Internet sources. When the array list has been ordered, it is then PRINTed with the final loop in the program.
All seems to be working just fine now. We can add or delete students from our DATA, and the program will adjust the DIMensions of the array using the cntr value, which is also used for READing and PRINTing. This program to print a simple list is finished.
READ and DATA are useful for loading variables used by the program, without having to type them in every time we boot. READ and DATA are not very useful if the data will change frequently, or if the list must be edited and maintained by a user with little or no programming skills.
To make this program still more flexible, and allow the data to be easily maintained by any user, we need to learn the basics of working with a data file. More on that another time.
Welopez